L.Control.MousePosition = L.Control.extend({
  options: {
    position: 'bottomleft',
    separator: ' : ',
    emptyString: '鼠标经纬度',
    lngFirst: false,
    numDigits: 5,
    lngFormatter: lngFormatter,
    latFormatter: latFormatter,
    prefix: ""
  },

  onAdd: function (map) {
    this._container = L.DomUtil.create('div', 'leaflet-control-mouseposition');
    L.DomEvent.disableClickPropagation(this._container);
    map.on('mousemove', this._onMouseMove, this);
    this._container.innerHTML=this.options.emptyString;
    return this._container;
  },

  onRemove: function (map) {
    map.off('mousemove', this._onMouseMove)
  },

  _onMouseMove: function (e) {
    var lng = this.options.lngFormatter ? this.options.lngFormatter(e.latlng.lng) : L.Util.formatNum(e.latlng.lng, this.options.numDigits);
    var lat = this.options.latFormatter ? this.options.latFormatter(e.latlng.lat) : L.Util.formatNum(e.latlng.lat, this.options.numDigits);
    var value = this.options.lngFirst ? lng + this.options.separator + lat : lat + this.options.separator + lng;
    var prefixAndValue = this.options.prefix + ' ' + value;
    this._container.innerHTML = prefixAndValue;
  }

});

//格式化经度 by  商勇
function lngFormatter(lng){
	
	var longSeconds = lng * 3600;
    var longDegrees = parseInt(longSeconds / 3600);
    longSeconds = Math.abs(longSeconds % 3600);
    var longMinutes = parseInt(longSeconds / 60);
    longSeconds %= 60;
    longSeconds=L.Util.formatNum(longSeconds,2);
    var lonDegrees = longDegrees >= 0 ? "E" : "W";
    
    return Math.abs(longDegrees) + "°" + longMinutes
    + "'" + longSeconds + "\"" + lonDegrees;
	
};

//格式化纬度  by  商勇
function latFormatter(lat){
	
    var latSeconds = lat * 3600;
    var latDegrees = parseInt(latSeconds / 3600);
    latSeconds = Math.abs(latSeconds % 3600);
    var latMinutes = parseInt(latSeconds / 60);
    latSeconds %= 60;
    latSeconds=L.Util.formatNum(latSeconds,2);
    
    var latDegree = latDegrees >= 0 ? "N" : "S";
    
    return  Math.abs(latDegrees) + "°" + latMinutes + "'" + latSeconds
    + "\"" + latDegree ;
    
    
};

L.Map.mergeOptions({
    positionControl: false
});

L.Map.addInitHook(function () {
    if (this.options.positionControl) {
        this.positionControl = new L.Control.MousePosition();
        this.addControl(this.positionControl);
    }
});

L.control.mousePosition = function (options) {
    return new L.Control.MousePosition(options);
};
